package week4.day25;

import java.util.concurrent.TimeUnit;

/**
 * @Author fengzihao
 * @Date 2024-05-09 19:47
 */
public class AlternatePrintByThread {
    public static void main(String[] args) throws InterruptedException {
        Object object = new Object();
        Thread thread1 = new Thread(()->{
            int num = 1;
            synchronized (object){
                while (num<8){
                    object.notify();
                    System.out.print(num++);
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                object.notify();
            }
        });
        Thread thread2 = new Thread(()->{
            char ch = 'A';
            synchronized (object){
                while (ch<'H'){
                    object.notify();
                    System.out.print(ch++);
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                object.notify();
            }
        });
        thread1.start();
        TimeUnit.MILLISECONDS.sleep(100);
        thread2.start();
    }
}